package com.demo.jdbc;




import com.mysql.jdbc.Driver;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import org.omg.CORBA.portable.InputStream;

/**
 * @ClassName JDBCTest
 * @Description: TODO
 * @Author Admin
 * @Date 2020/5/22
 * @Version V1.0
 **/
public class JDBCTest {

  public static void main(String[] args) throws SQLException {
    //注册驱动   new Driver()在该处只是为了加载类，依赖性较强，所以通过反射的方法去加载类  new Driver 是在编译期加载类
    try {
      //是在运行期加载的
      Class<Driver> deiver = (Class<Driver>) Class.forName("com.mysql.jdbc.Driver");
     /* Connection connection = DriverManager
          .getConnection("jdbc:mysql://localhost:3306/hahh?user=root&password=root");
      System.out.println("获取连接成功");*/
      String inputuser = new Scanner(System.in).next();
      Properties pro = new Properties();
      pro.load(new FileInputStream("src\\main\\resources\\application.properties"));
      String user = pro.getProperty("user");
      String url = pro.getProperty("url");
      String password = pro.getProperty("password");
      Connection connection1 = DriverManager.getConnection(url, user, password);
      //万事万物皆对象  Statement 执行sql的对象类
     /* Statement statement = connection1.createStatement();
      // Statement 会引发sql注入问题（字符串的拼接）
      String sql = "select * from admin where name like '"+inputuser+"%'";*/
      String presql = "select * from admin where name = ?";
      //PreparedStatement 通过？的占位符进行了预编译
      PreparedStatement preparedStatement = connection1.prepareStatement(presql);
      preparedStatement.setString(1,inputuser);
      //返回表格形式的结果集
      ResultSet resultSet = preparedStatement.executeQuery();
      List<Integer> idlist =new ArrayList<Integer>();
      List<String> namelist =new ArrayList<String>();
      List<String> passwordlist =new ArrayList<String>();
     while (resultSet.next()){
       int id = resultSet.getInt("id");
       idlist.add(id);
       String name = resultSet.getString("name");
       namelist.add(name);
       String password1 = resultSet.getString("password");
       passwordlist.add(password1);
     }
      System.out.println(idlist.toString()+"\t"+namelist.toString()+"\t"+passwordlist.toString());
     //关闭连接
      resultSet.close();
      preparedStatement.close();
      connection1.close();


    } catch (ClassNotFoundException | FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    DriverManager.deregisterDriver(new Driver());



  }



}
